//
//  FixThrowRule.m
//  HaoDeCaiFu
//
//  Created by admin on 2018/1/8.
//  Copyright © 2018年 郑开宏. All rights reserved.
//

#import "FixThrowRule.h"
#define ViewHeight 260

static id _publishContent;//类方法中的全局变量这样用（类型前面加static）
//static UIVisualEffectView *_effectView;//毛玻璃效果
static UIView *_effectView;
static UIPageControl * imagePC;//跟随scrollview滚动

static NSArray * contentArr;
static float heightAll;
static UIButton * cancelBut;

@implementation FixThrowRule

+(void)shareWithContent:(NSArray *)content title:(NSString *)title
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //毛玻璃特效类型
    //UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //_effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
    _effectView=[[UIView alloc]init];
    _effectView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    //设置模糊透明度
    _effectView.alpha = 0.8f;
    [window addSubview:_effectView];
    
    
    /**
     点击退出手势
     */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [_effectView addGestureRecognizer:tap];
    
    
    /**
     加上一层黑色透明效果
     */
    UIView *blackV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    blackV.backgroundColor = [UIColor grayColor];
    blackV.alpha = 0.5f;
    [_effectView addSubview:blackV];
    
    
    
    UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(40, screenHeight-ViewHeight, screenWidth-80, ViewHeight)];
    shareView.tag = 441;
    shareView.layer.masksToBounds = YES;
    shareView.layer.cornerRadius = 5.0;
    shareView.clipsToBounds = NO;
    shareView.backgroundColor = [UIColor whiteColor];
    [window addSubview:shareView];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(0, 0, shareView.width, 40);
    titleLabel.text = title;
    titleLabel.layer.masksToBounds = YES;
    titleLabel.layer.cornerRadius = 5.0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:16.0];
    titleLabel.backgroundColor = RGB(233, 233, 233);
    [shareView addSubview:titleLabel];
    
    
    UILabel * lineLabel = [[UILabel alloc]init];
    lineLabel.frame = CGRectMake(0,titleLabel.bottom-1,shareView.width - 40,0.5);
    lineLabel.backgroundColor = RGB(204, 204, 204);
    [shareView addSubview:lineLabel];
    
    heightAll = 0.0;
    //float labelHeight = (ViewHeight - titleLabel.height)/3.0;
    for (int i = 0; i < content.count; i++) {
        float heights = [self returnLabelHeight:content[i] labelWidth:shareView.width - 40 fontSize:14.0];
        
        UILabel * contentLabel = [[UILabel alloc]init];
        contentLabel.frame = CGRectMake(20, titleLabel.bottom + 20 +heightAll, shareView.width - 40, heights);
        contentLabel.text = content[i];
        contentLabel.font = [UIFont systemFontOfSize:14.0];
        contentLabel.textColor = RGB(51, 51, 51);
        contentLabel.numberOfLines = 0;
        [shareView addSubview:contentLabel];
        
        heightAll += heights+20;
    }
    
    
    //取消按钮
    cancelBut = [[UIButton alloc]init];
    cancelBut.bounds = CGRectMake(0, 0, 30, 30);
    cancelBut.center = CGPointMake(shareView.left+shareView.width, shareView.top+30/2);
    cancelBut.backgroundColor = [UIColor whiteColor];
    [cancelBut setBackgroundImage:[UIImage imageNamed:@"img_cancel"] forState:UIControlStateNormal];
    cancelBut.layer.masksToBounds = YES;
    cancelBut.layer.cornerRadius = cancelBut.height/2.0;
    [cancelBut addTarget:self action:@selector(cancelButClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:cancelBut];
    
    
    //为了弹窗不那么生硬，这里加了个简单的动画
    shareView.frame = CGRectMake(40, screenHeight, screenWidth-80, titleLabel.height + heightAll+20);
    //shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
    //_effectView.contentView.alpha = 0;
    _effectView.alpha = 0;
    [UIView animateWithDuration:0.35f animations:^{
        shareView.frame = CGRectMake(40, (screenHeight-shareView.height)/2, screenWidth-80, shareView.height);
        cancelBut.center = CGPointMake(shareView.width, 0);
        //shareView.transform = CGAffineTransformMakeScale(1, 1);
        //_effectView.contentView.alpha = 1;
        _effectView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
}
+(void)cancelButClick
{
    [self dismiss];
}
+ (void)dismiss {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *shareView = [window viewWithTag:441];
    
    //为了弹窗不那么生硬，这里加了个简单的动画
    shareView.transform = CGAffineTransformMakeScale(1, 1);
    [UIView animateWithDuration:0.35f animations:^{
        shareView.frame = CGRectMake(40, screenHeight, screenWidth-80, ViewHeight);
        cancelBut.center = CGPointMake(shareView.width, shareView.top+30/2);
        //shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
        //_effectView.contentView.alpha = 0;
        _effectView.alpha = 0;
    } completion:^(BOOL finished) {
        
        [shareView removeFromSuperview];
        [_effectView removeFromSuperview];
        [cancelBut removeFromSuperview];
        [imagePC removeFromSuperview];
    }];
}

//+(void)shareImgClick:(UITapGestureRecognizer *)recognizer
//{
//    LLLog(@"点击图片的tag是:%ld",(long)recognizer.view.tag);
//    NSString * URLString = [array[(long)recognizer.view.tag-100] objectForKey:@"URL"];
//    if (URLString.length != 0 && [[array[(long)recognizer.view.tag-100] objectForKey:@"IS_Click"] isEqualToString:@"0"]) {
//        [self dismiss];
//    }
//
//
//    if (clickBlocks != nil) {
//        clickBlocks((long)recognizer.view.tag);
//    }
//}
+(float)returnLabelHeight:(NSString *)string labelWidth:(float)width fontSize:(float)sizes
{
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{                                                                NSFontAttributeName : [UIFont systemFontOfSize:sizes]                                                          } context:nil].size;
    CGFloat nameH = size.height;
    //CGFloat nameW = size.width;
    return nameH;
}
+(void)returnClickIndexBlock:(ReturnClickIndex)clickBlock
{
    clickBlocks = clickBlock;
}

@end
